using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using AmazonProductAdvtApi;

namespace CueCatDecoder
{

    public class AmazonBook : Product
    {
        public string Author;
        public string Publisher;
        public string Title;
        public string AmazonUrl;

        public AmazonBook(string author, string publisher, string title, string amazonurl, string id)
            : base(id, IdType.ISBN)
        {
            Author = author;
            Publisher = publisher;
            Title = title;
            AmazonUrl = amazonurl;
        }
    }

    public class AmazonBookPopulator
    {
        private const string DESTINATION = "ecs.amazonaws.com";        
        private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2005-10-05";

        public static AmazonBook CreateAmazonProduct(Product product, string AWSKey, string AWSSecretKey)
        {
            SignedRequestHelper helper = new SignedRequestHelper(AWSKey, AWSSecretKey, DESTINATION); // HACK:            


            string amazonType = "ASIN";
            if (product.Type == CueCatDecoder.IdType.UPC)
            {
                amazonType = "UPC";
            }
            
            string amazonUrl = String.Format("Service=AWSECommerceService&AWSAccessKeyId={0}&Operation=ItemLookup&ItemId={1}&IdType={2}&SearchIndex=",                    
                    AWSKey,
                    product.ID,
                    amazonType);
            if (product.Type == CueCatDecoder.IdType.UPC)
            {
                amazonUrl += "Books";
            }

            amazonUrl = helper.Sign(amazonUrl);
            
            return FetchBookInfo(amazonUrl, product.ID);
        }

        private static AmazonBook FetchBookInfo(string url, string productId)
        {
            try
            {
                WebRequest request = HttpWebRequest.Create(url);
                WebResponse response = request.GetResponse();
                XmlDocument doc = new XmlDocument();
                doc.Load(response.GetResponseStream());

                XmlNodeList errorMessageNodes = doc.GetElementsByTagName("Message");
                if (errorMessageNodes != null && errorMessageNodes.Count > 0)
                {
                    String message = errorMessageNodes.Item(0).InnerText;
                   //return "Error: " + message + " (but signature worked)";
                }

                XmlNode titleNode = doc.GetElementsByTagName("Title", NAMESPACE).Item(0);
                string title = titleNode.InnerText;

                XmlNode publisherNode = doc.GetElementsByTagName("Manufacturer", NAMESPACE).Item(0);
                string publisher = publisherNode.InnerText;

                XmlNode authorNode = doc.GetElementsByTagName("Author", NAMESPACE).Item(0);
                string author = authorNode.InnerText;

                XmlNode pageUrlNode = doc.GetElementsByTagName("DetailPageURL", NAMESPACE).Item(0);
                string pageUrl = pageUrlNode.InnerText;

                return new AmazonBook(author, publisher, title, pageUrl, productId);
                
                            }
            catch (Exception e)
            {
                System.Console.WriteLine("Caught Exception: " + e.Message);
                System.Console.WriteLine("Stack Trace: " + e.StackTrace);
            }

            return null;
        }


    }

}
